# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils

MY_P=${P/0./}
MY_P=${MY_P/-/.}

DESCRIPTION="Digital Mars D Programming Language Compiler"
HOMEPAGE="http://www.digitalmars.com/d/"
SRC_URI="http://ftp.digitalmars.com/${MY_P}.zip"

LICENSE="DMD"
RESTRICT="mirror"
SLOT="${PV}"
KEYWORDS="~x86 ~amd64"
IUSE=""

MY_D="/opt/dmd/${SLOT}"
S="${WORKDIR}/${PM}"

RDEPEND=""

DEPEND="${RDEPEND}
	virtual/libc
	>=sys-devel/gcc-2.3.0
	amd64? ( app-emulation/emul-linux-x86-compat )"

src_unpack() {
	unpack ${A}

	cd "${S}"

	# remove Windows files
	rm -r dm
	rm dmd/lib/*.LIB dmd/lib/*.lib
	rm dmd/bin/*.exe dmd/bin/sc.ini
	if [ -f dmd/bin/dm.dll ]; then
		rm dmd/bin/*.dll
	fi
	if [ -f dmd/bin/windbg.hlp ]; then
		rm dmd/bin/windbg.hlp
	fi
}

src_install() {
	cd "${S}/dmd"

	# cleanup line ends
	edos2unix bin/dmd.conf
	edos2unix `find . -name '*.c' -type f`
	edos2unix `find . -name '*.d' -type f`
	edos2unix `find . -name '*.h' -type f`
	edos2unix `find . -name '*.txt' -type f`

	# configuration
	mv bin/dmd bin/dmd-${PV}.bin

	if use x86; then
		cat <<EOF > "bin/dmd-${PV}"
#!/bin/bash
${MY_D}/bin/dmd-${PV}.bin -I${MY_D}/src/phobos -L-L${MY_D}/lib \$*
EOF
	else
		cat <<EOF > "bin/dmd-${PV}"
#!/bin/bash
CC=gcc32 ${MY_D}/bin/dmd-${PV}.bin -I${MY_D}/src/phobos -L-L${MY_D}/lib \$*
EOF
	fi

	# fix permissions
	chmod guo=r `find . -type f`
	chmod guo=rx bin/dmd-${PV}* bin/dumpobj bin/obj2asm bin/rdmd
	chmod guo=rx `find . -type d`

	# documentation
	dodoc license.txt
	dodoc src/phobos/phoboslicense.txt
	dodoc html/d/changelog.html
	doman man/man1/dmd.1
	doman man/man1/dumpobj.1
	doman man/man1/obj2asm.1

	# install
	mkdir -p "${D}/${MY_D}" || die
	cp -pRvf "${S}/dmd/*" "${D}/${MY_D}"
	mkdir -p "${D}/usr/bin" || die
	dosym "${MY_D}/bin/dmd-${SLOT}" "${MY_D}/bin/dmd"
	dosym "${MY_D}/bin/dmd-${SLOT}" "/usr/bin/dmd"
}
